<?php 
$GLOBALS['Md'] = "TimeClock";
$GLOBALS['Pd'] = "B";
require_once($_SERVER['DOCUMENT_ROOT']."/defaultconf.mex");
print heade(); 
print body(); 
module($BSD['DB00']);
office_header($_SESSION[$SESSOF]);
include($GLOBALS['PAD']."Modules/TimeClock/main.obj.php");
include($GLOBALS['PAD']."Modules/TimeClock/nav.views.php");
include($GLOBALS['PAD']."Modules/CentrodeCustos/func.php");
print NavBack();

	$sysTime = date("Y-m-d H:i:s");

if (isset($_POST['Save'])) {
	$Udate = strtotime($_POST['TimeStart']);
	$Mon = 'Mon'.date("w",$Udate);
				
				$QAction = "INSERT timeclock_vacs
				(RefUser, TimeStart, TimeEnd, Status, Days, Observations, Justification, Type, RefOffice, Time, sysUser) 
				VALUES ({$_POST['User']},'{$_POST['TimeStart']}','{$_POST['TimeStart']}',4,1,'Marca��o Recursos Humanos','{$_POST['Justification']}',2,{$_POST['Office']},'{$sysTime}',{$_SESSION['UtID']})";
				$Action = $GLOBALS['CONN']->db_query($QAction);
				 
		$QProfiles = "SELECT 
		timeclock_profiles.ref,
		timeclock_profiles.main_users_ref AS USER,
		timeclock_profiles.delay,
		timeclock_profiles.{$Mon},
		timeclock_profiles.ProfileType AS Type,
		timeclock_ttables.entrytime,
		timeclock_ttables.ref AS Tabs
			FROM timeclock_profiles
		INNER JOIN timeclock_ttables ON timeclock_ttables.ref = timeclock_profiles.{$Mon}
		WHERE timeclock_profiles.main_users_ref = {$_POST['User']}";
	$Profiles = $GLOBALS['CONN']->db_array($QProfiles); 
	for ($r = 1; $r <= $Profiles[0]; $r++) { 
		
		
		$QOp = "INSERT INTO timeclock_operations 
							(main_users_ref, time_main, RefTTable)
							 VALUES 
							 ({$_POST['User']}, '{$sysTime}', {$Profiles[$r]['Tabs']})";
							$GLOBALS['CONN']->db_query($QOp); 

		$QOp = "SELECT ref FROM timeclock_operations WHERE 
							time_main = '{$sysTime}' AND main_users_ref = {$_POST['User']}";
							$Op = $GLOBALS['CONN']->db_array($QOp); 
							
		$QO = "INSERT INTO timeclock_delays 
				(main_user_ref, delaylevel, delaytime, EventTime, Status, RefType, RefOperation) 
				VALUES 
				({$Profiles[$r]['USER']}, 0, 0, '{$_POST['TimeStart']}', 1, 2, {$Op[1]['ref']})";
				
		}
				
				if (isset($Action)) {
					$Msg_CONFIRM = "Pedido Submetido";
					$_GET['VCREQ'] = 'DONE';
				}
}
if (isset($_POST['Aprov'])) {

	$Justifications = $_POST['Justification'];
	$VcRequests = $_POST['Aprov'];
	$CReq = count($VcRequests);
	for ($r = 0; $r < $CReq; $r++) {
		$VcR = split("-",$VcRequests[$r]);
		if (isset($VcR) && $VcR[1] != '1') {
				$UAction = "UPDATE timeclock_vacs SET Status = {$VcR[1]}, Justification = '{$Justifications[$r]}' WHERE ref = {$VcR[0]}";
				$Action = $GLOBALS['CONN']->db_query($UAction); 
				
				if (isset($Action)) {
					$Msg_CONFIRM = "Pedido Submetido";
				}
		}
	}

} 
?>
<script charset="utf-8" type="text/javascript" src="../../../Shared/Libraries/JS/calendar.js"></script>
<style type="text/css">
<!--
.style1 {color: #000000}
-->
</style>
 
<table width="100%"  border="0" cellpadding="10">
  <tr>
    <td>
      <table width="100%"  border="0" cellpadding="3" cellspacing="1">
        <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="get" name="getData">
          <tr bgcolor="#99CCCC">
            <td bgcolor="#ACB1CC" colspan="2">
			<input name="VCREQ" alt="Marcar Férias" value="Marcar Falta" type="button" onClick="getscreen('<?php echo $_SERVER['PHP_SELF'];?>?VCREQ=VCONF');"></td>
          </tr>
        </form>
		<?php if (isset($_GET['VCREQ']) && $_GET['VCREQ'] == 'VCONF') { ?>
	  <form name="SetVacs" action="<?php echo $_SERVER['PHP_SELF'];?>?VCREQ=VCONF" method="post" runat="server">
        <tr bgcolor="#ACB1CC">
          <td colspan="2"><span class="style1">Pedido de Falta </span></td>
        </tr>
        <tr bgcolor="#F2F2F2">
          <td width="20%">Loja:</td>
          <td><?php echo SelectOffice('SetVacs','Office');?></td>
        </tr>
        <tr bgcolor="#F2F2F2">
          <td>Funcionário:</td>
          <td><?php if (!isset($_POST['Office'])) { $_POST['Office'] = 0; }
		   SelectUser($_POST['Office'], 'Office', 'SetVacs', 'User') ?></td>
        </tr>
        <tr bgcolor="#F2F2F2">
          <td>Data:</td>
          <td><input name="TimeStart" type="text" onFocus="this.select();lcs(this)" 
	  onClick="event.cancelBubble=true;this.select();lcs(this)" 
	  value="<?php if (isset($_POST['TimeStart'])) {
	  print $_POST['TimeStart']; } else { print "AAAAMMDD";} ?>" size="14" /></td>
        </tr>
		<tr bgcolor="#F2F2F2">
		  <td valign="top">ObservaÇ&otilde;es:</td>
		  <td><textarea name="Justification" cols="50" rows="3" id="Justification"></textarea></td>
		  </tr>
        <tr bgcolor="#F2F2F2">
          <td>&nbsp;</td>
          <td><input name="Save" type="submit" id="Save" value="Submeter"></td>
        </tr>
		</form>
      </table>	 
	  <?php } 
	  ?> <br>
	  
	  <table width="100%"  border="0" cellpadding="3" cellspacing="1">
      <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="get" name="getData">
        <tr bgcolor="#99CCCC">
          <td bgcolor="#ACB1CC" colspan="7"><select name="ViewMonth" id="ViewMonth">
    <?php 
	if (!isset($_GET['ViewMonth'])) {
		$_GET['ViewMonth'] = date("m");
		} 
	if (!isset($_GET['ViewYear'])) {
		$_GET['ViewYear'] = date("Y");
		} 
	$QMonths = "SELECT MONTH(TimeStart) AS Month FROM timeclock_vacs
	GROUP BY MONTH(TimeStart)";
	$Months = $GLOBALS['CONN']->db_array($QMonths); 
	for ($o = 1; $o <= $Months[0]; $o++) { 
	$Tim = mktime(0,0,0,$Months[$o]['Month'],15,0);?>
            <option value="<?php echo $Months[$o]['Month'];?>" <?php if ($_GET['ViewMonth'] == $Months[$o]['Month']) { print "selected"; }?>>
            <?php echo date("M",$Tim);?>
            </option>
            <?php } ?>
          </select>
            <select name="ViewYear" id="select2">
              <?php 
	$QMonths = "SELECT YEAR(TimeStart) AS Year FROM timeclock_vacs
	GROUP BY YEAR(TimeStart)";
	$Months = $GLOBALS['CONN']->db_array($QMonths); 
	for ($o = 1; $o <= $Months[0]; $o++) { 
	$Tim = mktime(0,0,0,6,15,$Months[$o]['Year']);?>
              <option value="<?php echo $Months[$o]['Year'];?>" <?php if ($_GET['ViewYear'] == $Months[$o]['Year']) { print "selected"; }?>>
              <?php echo date("Y",$Tim);?>
              </option>
              <?php } ?>
            </select>            
	<select name="Offices">
	<option value="">Todos</option>
	<?php
	$QCallOffice = "SELECT * FROM main_offices WHERE ref_domain = '1' ORDER BY address_city ASC";
	$CallOffice = $GLOBALS['CONN']->db_array($QCallOffice); 
	for ($o = 1; $o <= $CallOffice[0]; $o++) {
	?>
       <option value="<?php echo $CallOffice[$o]['office_number'];?>" <?php if (isset($_GET['Offices']) && $_GET['Offices'] == $CallOffice[$o]['office_number']) { print "selected";}?>><?php echo $CallOffice[$o]['name'];?></option>
	<?php } ?>
            </select>
			<select name="Status">
	<option value="">Todos</option>
    <option value="1" <?php if (isset($_GET['Status']) && $_GET['Status'] == 1) { print "selected";}?>>A Aguardar</option>
	<option value="2" <?php if (isset($_GET['Status']) && $_GET['Status'] == 2) { print "selected";}?>>Rejeitado</option>
	<option value="4" <?php if (isset($_GET['Status']) && $_GET['Status'] == 4) { print "selected";}?>>Aceite</option>
            </select><input type="submit" name="Submit" value="Consultar"></td>
        </tr>
      </form>
	
        <tr bgcolor="#F2F2F2">
          <td width="15%">Funcionário</td>
          <td width="15%">Departamento</td>
          <td width="10%">Início</td>
          <td width="10%">Fim</td>
          <td width="20%">ObservaÇ&otilde;es</td>
          <td width="20%">Justificação</td>
          <td width="10%">Estado</td>
        </tr>
   <?php
	    if (isset($_GET['Offices']) && $_GET['Offices'] != "") { 
			$Offices = " AND main_offices.ref = {$_GET['Offices']}";
			} else {
			$Offices = "";
			}
	    if (isset($_GET['Status']) && $_GET['Status'] != "") { 
			$Status = " AND timeclock_vacs.Status = {$_GET['Status']}";
			} else {
			$Status = "";
			}
		$CVacs = "SELECT 
		   				
						timeclock_vacs.ref AS vcref,
						timeclock_vacs.TimeStart,
						timeclock_vacs.TimeEnd,
						timeclock_vacs.Days,
						timeclock_vacs.Observations,
						timeclock_vacs.Justification,
						timeclock_vacs.Status,
						main_offices.name AS office, main_users.name AS name,
						 timeclock_profiles.ref,
						 timeclock_profiles.main_users_ref,
						 timeclock_profiles.delay,
						 timeclock_profiles.mon1,
						 timeclock_profiles.mon2,
						 timeclock_profiles.mon3,
						 timeclock_profiles.mon4,
						 timeclock_profiles.mon5,
						 timeclock_profiles.mon6,
						 timeclock_profiles.mon0,
						 timeclock_profiles.ProfileType
				FROM timeclock_vacs
		INNER JOIN timeclock_profiles ON timeclock_profiles.main_users_ref = timeclock_vacs.RefUser
		INNER JOIN main_users ON main_users.ref = timeclock_profiles.main_users_ref
		INNER JOIN main_offices ON main_users.ref_office = main_offices.ref
		WHERE timeclock_vacs.Type = 2
		AND ((MONTH(timeclock_vacs.TimeStart) = {$_GET['ViewMonth']} AND YEAR(timeclock_vacs.TimeStart) = {$_GET['ViewYear']})
			OR (MONTH(timeclock_vacs.TimeEnd) = {$_GET['ViewMonth']} AND YEAR(timeclock_vacs.TimeEnd) = {$_GET['ViewYear']}))
		{$Status}
		{$Offices}";
		$Vacs = $GLOBALS['CONN']->db_array($CVacs);
		for ($f = 1; $f <= $Vacs[0]; $f++) { ?>
        <tr bgcolor="#F2F2F2">
     <td><?php echo $Vacs[$f]['name'];?></td>
          <td><?php echo $Vacs[$f]['office'];?></td>
          <td><?php $Start = split(" ",$Vacs[$f]['TimeStart']);
		  			print $Start[0]; ?></td>
          <td><?php $End = split(" ",$Vacs[$f]['TimeEnd']);
		  			print $End[0]; ?></td>
          <td width="15%"><?php echo $Vacs[$f]['Observations'];?></td>
          <td width="15%"><?php echo $Vacs[$f]['Justification'];?></td>
          <td width="15%"><?php
		  if ($Vacs[$f]['Status'] == '1') {print 'A Aguardar';}
		    if ($Vacs[$f]['Status'] == '2') {print 'Rejeitado';}
			  if ($Vacs[$f]['Status'] == '3') {print 'Pendente';}
			    if ($Vacs[$f]['Status'] == '4') {print 'Aceite';} ?>
         </td>
        </tr>
		<?php  } ?>
    </table>
	
	<br>	<table width="100%"  border="0" cellpadding="3" cellspacing="1">
	<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" runat="server">
        <tr bgcolor="#ACB1CC">
          <td colspan="8">Pedidos para aprovação </td>
        </tr>
        <tr bgcolor="#F2F2F2">
          <td width="15%">Aprovar</td>
          <td width="20%">Funcionário</td>
          <td width="20%">Departamento</td>
          <td width="15%">Início</td>
          <td width="15%">Fim</td>
		  <td width="15%">Faltas</td>
		  <td width="28%">ObservaÇ&otilde;es</td>
		  <td width="20%">Justificação</td>
		  </tr>
   <?php
	    $CVacs = "SELECT 
		   				
						timeclock_vacs.ref AS vcref,
						timeclock_vacs.TimeStart,
						timeclock_vacs.TimeEnd,
						timeclock_vacs.Days,
						timeclock_vacs.Status,
						timeclock_vacs.Observations,
						main_offices.name AS office, main_users.name AS name,
						 timeclock_profiles.ref,
						 timeclock_profiles.main_users_ref,
						 timeclock_profiles.delay,
						 timeclock_profiles.mon1,
						 timeclock_profiles.mon2,
						 timeclock_profiles.mon3,
						 timeclock_profiles.mon4,
						 timeclock_profiles.mon5,
						 timeclock_profiles.mon6,
						 timeclock_profiles.mon0,
						 timeclock_profiles.ProfileType
				FROM timeclock_vacs
		INNER JOIN timeclock_profiles ON timeclock_profiles.main_users_ref = timeclock_vacs.RefUser
		INNER JOIN main_users ON main_users.ref = timeclock_profiles.main_users_ref
		INNER JOIN main_offices ON main_users.ref_office = main_offices.ref
		WHERE timeclock_vacs.Type = 2
		AND timeclock_vacs.Status < 2";
		$Vacs = $GLOBALS['CONN']->db_array($CVacs);
		for ($f = 1; $f <= $Vacs[0]; $f++) { ?>
	    <tr bgcolor="#F2F2F2">
          <td><select name="Aprov[]" id="Aprov[]">
              <option value="<?php echo $Vacs[$f]['vcref'];?>-1" <?php if ($Vacs[$f]['Status'] == '1') {print 'selected';} ?>>Sem Acção</option>
              <option value="<?php echo $Vacs[$f]['vcref'];?>-4" <?php if ($Vacs[$f]['Status'] == '4') {print 'selected';} ?>>Aceite</option>
              <option value="<?php echo $Vacs[$f]['vcref'];?>-2" <?php if ($Vacs[$f]['Status'] == '2') {print 'selected';} ?>>Rejeitado</option>
              <option value="<?php echo $Vacs[$f]['vcref'];?>-3" <?php if ($Vacs[$f]['Status'] == '3') {print 'selected';} ?>>Pedente</option>
            </select></td>
          <td><?php echo $Vacs[$f]['name'];?></td>
          <td><?php echo $Vacs[$f]['office'];?></td>
          <td><?php $Start = split(" ",$Vacs[$f]['TimeStart']);
		  			print $Start[0]; ?></td>
          <td><?php $End = split(" ",$Vacs[$f]['TimeEnd']);
		  			print $End[0]; ?></td>
		  <td></td>
		  <td><?php echo $Vacs[$f]['Observations'];?></td>
		  <td><textarea name="Justification[]" cols="25" id="Justification[]"></textarea></td>
		  </tr>
	<?php } ?>
		 <tr bgcolor="#F2F2F2">
           <td><input type="submit" name="VacAprov" value="Executar"></td>
		 </tr>
	    </form>
    </table>    </tr>
</table>
<?php print footer(); ?>